package com.devplatform.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.devplatform.pojo.CommentLike;
import com.devplatform.pojo.dto.CommentPublishDto;
import com.devplatform.pojo.dto.CommentsDto;
import com.devplatform.pojo.vo.DataVo;
import com.devplatform.service.article.CommentsService;
import com.devplatform.util.UserContextHolder;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("api/comments")
@RequiredArgsConstructor
public class CommentsController {
    private final CommentsService commentsService;

    // 发布评论
    @PostMapping("/")
    public DataVo publish(@RequestBody CommentPublishDto dto) {
        commentsService.publish(dto);
        return DataVo.ok();
    }

    @GetMapping("/getComments/{articleId}")
    public DataVo getCommentList(@PathVariable Long articleId,
                                 @RequestParam(defaultValue = "1") Integer page,
                                 @RequestParam(defaultValue = "10") Integer size
    ) {
        IPage<CommentsDto> commentsDtoIPage = new Page<>(page, size);
        return commentsService.getCommentList(commentsDtoIPage, articleId);
    }

    //删除评论
    @DeleteMapping("/{commentId}")
    public DataVo delete(@PathVariable Integer commentId) {
        commentsService.delete(commentId);
        return DataVo.ok();
    }

    // 点赞评论
    @PostMapping("/like")
    public DataVo likeComment(@RequestBody CommentLike commentLike) {
        Long userId = UserContextHolder.getUserId();
        if (userId == null) {
            return DataVo.error("请先登录");
        }
        commentLike.setUserId(userId);
        return commentsService.like(commentLike);
    }

    // 检查用户是否已点赞某评论
    @GetMapping("/like/status/{commentId}")
    public DataVo<Boolean> checkLikeStatus(@PathVariable Long commentId) {
        Long userId = UserContextHolder.getUserId();
        return commentsService.checkLikeStatus(userId, commentId);
    }

    // 举报评论
    @PostMapping("/report/{commentId}")
    public DataVo report(@PathVariable Integer commentId) {
        commentsService.report(commentId);
        return DataVo.ok();
    }
}
